
import 'normalize.css';
import 'expose?$!expose?jQuery!jquery'
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/js/bootstrap';
import 'angular-material/angular-material.min.css'
import angular from 'angular';
import "angular-messages";
import "angular-animate";
import "angular-aria";
import 'ng-file-upload'
import 'angular-translate';
import '../fileManager/src/js/app';
import angular_material from 'angular-material';
import uiRouter from 'angular-ui-router';
import ui_bootstrap from "angular-ui-bootstrap";
import ocLazyLoad from 'oclazyload'
import ylc from './ylc-common';
import AppComponent from './app.component';

import AuthComponent from './components/auth';
angular.module('app', [
  uiRouter,
  ocLazyLoad, ylc, "ui.bootstrap", 'ngMaterial', 'FileManagerApp'
])
  .config(($locationProvider, $stateProvider, $urlRouterProvider) => {
    "ngInject";

    //$locationProvider.html5Mode(false).hashPrefix('!');

    $stateProvider
      .state('app', {
        url: '/app',
        abstract: true,
        template: '<div ui-view></div>'
      })
      .state('app.auth', {
        url: '/auth',
        template: '<auth></auth>'
      })
    $urlRouterProvider.otherwise('/app/auth');
  }).config(($httpProvider) => {
    "ngInject";
    $httpProvider.defaults.withCredentials = true;
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    var param = function (obj) {
      var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
      for (name in obj) {
        value = obj[name];
        if (value instanceof Array) {
          for (i = 0; i < value.length; ++i) {
            subValue = value[i];
            fullSubName = name; // + '[' + i + ']';
            innerObj = {};
            innerObj[fullSubName] = subValue;
            query += param(innerObj) + '&';
          }
        } else if (angular.isDate(value)) {
          query += encodeURIComponent(name) + '=' + encodeURIComponent(value.getTime()) + '&';
        } else if (value instanceof Object) {
          for (subName in value) {
            subValue = value[subName];
            fullSubName = name + '.' + subName;
            innerObj = {};
            innerObj[fullSubName] = subValue;
            query += param(innerObj) + '&';
          }
        } else if (value !== undefined && value !== null)
          query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
      }
      return query.length ? query.substr(0, query.length - 1) : query;
    }
    $httpProvider.defaults.transformRequest = [function (data) {
      return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }]
    $httpProvider.defaults.transformResponse = [function defaultHttpResponseTransform(data, headers) {
      if (angular.isString(data)) {
        var tempData = data.replace(/^\)]\}',?\n/, "").trim();
        if (tempData) {
          var contentType = headers("Content-Type");
          if (contentType && contentType.indexOf('application/json') === 0) {
            try {
              data = angular.fromJson(tempData);
            } catch (error) {
            }
          }
        }
      }
      return data;
    }]
    rootPathInterceptor.$inject = ['$q', '$injector'];

    function rootPathInterceptor($q, $injector) {
      var request = function (config) {
        var u = config.url;
        var s = u.charAt(0);
        if (s == ':') {
          config.url = '../../..' + u.substring(u.indexOf('/'))
        }
        return config;
      }
      var requestError = function (rejection) {
        return $q.reject(rejection);
      }
      return {
        'request': request,
        'responseError': requestError
      };
    }
    $httpProvider.interceptors.push(rootPathInterceptor);
  }).config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
  }])
  .component('app', AppComponent).component('auth', AuthComponent);
